<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="phone">
          <el-input
            v-model="loginForm.phone"
            prefix-icon="iconfont icon-user"
            maxlength="11"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="code" class="bech">
          <el-input
            v-model="loginForm.code"
            prefix-icon="iconfont icon-3702mima"
            type="number"
            maxlength="6"
            show-password
            placeholder="请输入验证码"
            @input="handleInput"
            style="width: 200px"
          ></el-input>
          <el-button type="primary" class="huo" @click="getCode" :disabled="daojishi > 0"
            >获取验证码 {{ daojishi > 0 ? daojishi + "秒" : "" }}
          </el-button>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login" :loading="loading"
            >登录</el-button
          >
          <el-button type="info" @click="resetLoginForm">密码登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { phoneSmsCodeLogin, getShopInfo, sendSmsCode } from "@/api/user.js";
export default {
  data() {
    return {
      loading: false,
      daojishi: 0,
      loginForm: {
        phone: "",
        code: "",
        smsToken: "",
      },
      loginFormRules: {
        // 验证手机号是否合法
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            min: 11,
            max: 11,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          {
            min: 6,
            max: 6,
            message: "请输入正确的验证码",
            trigger: "blur",
          },
          {
            pattern: /^\d{6}$/,
            message: "验证码必须为6位数字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //密码登录
    resetLoginForm() {
      this.$router.push("/login");
    },
    //验证码输入数字
    handleInput(event) {
      this.loginForm.code = this.loginForm.code.replace(/\D/g, "");
    },
    //获取验证码
    getCode() {
      this.$refs.loginFormRef.validateField("phone", (errorMessage) => {
        if (errorMessage) {
          this.$message.error(errorMessage);
        } else {
          const jsonObject = {
            phone: this.loginForm.phone,
          };
          sendSmsCode(jsonObject).then((res) => {
            if (res.code == 200) {
              this.$message.success("验证码发送成功");
              console.log(res);
              // this.loginForm.code = res.data.code
              this.loginForm.smsToken = res.data.smsToken;
              this.startCountdown(); // 开始倒计时
            }
          });
        }
      });
    },
    startCountdown() {
      this.daojishi = 60; // 设置倒计时为60秒
      const countdownInterval = setInterval(() => {
        if (this.daojishi > 0) {
          this.daojishi -= 1; // 每秒减1
        } else {
          clearInterval(countdownInterval); // 倒计时结束，清除定时器
        }
      }, 1000);
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const jsonObject = {
          smsCode: this.loginForm.code,
          phone: this.loginForm.phone,
          smsToken: this.loginForm.smsToken,
        };
        this.loading = true;
        phoneSmsCodeLogin(jsonObject).then((res) => {
          console.log(res);
          if (res.code !== "200") {
            this.$message.error("登录失败：" + res.msg);
            return;
          } else {
            localStorage.setItem("kua_token", res.data.sessionToken);
            getShopInfo()
              .then((res) => {
                if (!res.data) {
                  this.$message.error("请下载夸奖君APP开店，夸小店");
                  return;
                } else {
                  if (res.data.shop.status == 1) {
                    localStorage.setItem("shop", JSON.stringify(res.data.shop));
                    this.$message.success("登录成功");
                    this.$router.push("/classify");
                    return;
                  } else if (res.data.shop.status == 0) {
                    this.$message.error("您的店铺正在审核中~");
                    return;
                  } else {
                    this.$message.error("您的店铺审核未通过~");
                    return;
                  }
                }
              })
              .finally(() => {
                this.loading = false;
              });
          }
        });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;

    .btns {
      display: flex;
      justify-content: flex-end;
    }
    .huo{
      margin-left: 20px;
    }
  }
  ::v-deep .bech .el-form-item__content {
    width: 100%;
    display: flex;
  }
}
</style>